<?php
/**
 * CRMLite ContactHistory Controller
 *
 * @final
 * @package 	Crmlite_App
 * @version 	$Id: ContacthistoryController.php 68 2007-09-10 09:36:16Z etng2004 $
 * @copyright 	2006-2007 ETE.CN
 * @author 		Yi BO  <etng2004@gmail.com>
 * @license 	PHP Version 3.0 {@link http://www.php.net/license/3_0.txt}
 */
require_once dirname(__FILE__) . '/AdminBaseController.php';
final class Admin_ContactHistoryController extends AdminBaseController
{
    /**
	 * 默认的模型
	 *
	 * 未初始化时是字符串，初始化之后就是一个模型接口
	 *
	 * @var ContactHistoryModel
	 */
    protected $model = 'ContactHistory';
    protected $scaffholding = true;
    public function indexAction()
    {
        $this->_forward('list');
    }

    protected function dateRegion($field, $range, $prefix = null)
    {
         switch ($range)
        {
            case 'today':
                $case = "DATE(FROM_UNIXTIME({$prefix}.{$field})) = CURDATE()";
                break;
            case 'yestoday':
                $case = "DATE(FROM_UNIXTIME({$prefix}.{$field})) = DATE_SUB(CURDATE(),INTERVAL 1 DAY)";
                break;
            case 'week':
                 $case = "WEEK(DATE(FROM_UNIXTIME({$prefix}.{$field})), 1) = WEEK(CURDATE(), 1)";
                break;
            case 'month':
                $case = "MONTH(DATE(FROM_UNIXTIME({$prefix}.{$field}))) = MONTH(CURDATE())";
                break;
            case 'all':
            default:
                $case = '';
                break;
        }
        return $case;
    }
    public function detaillistAction()
    {
        $where = array();
        if($client = intval($this->_getParam('client', 0)))
        {
            $where []= "contact_histories.client_id = {$client}";
        }
        /**
		 * @todo 处理筛选条件并设置排序方式
		 */
        $order = null;
        /**
		 * @todo 处理筛选字段 并设置默认值
		 */
        /*		$default_statics = array();
        $this->view->statics = $this->model->statics($where, 'trade_type_id', $default_statics);*/
        $date_region  = $this->_getParam('date_region', 'ALL');
        $date_range = $this->dateRegion('contact_at', $date_region, 'contact_histories') ;
        if($date_range) $where []= $date_range;
        $this->view->date_region = $date_region;
        $pager = array(
        'curpage' 	=> $this->_getParam('page', 	1),
        'perpage'	=> $this->_getParam('perpage', 10),
        'total'		=> $this->model->count($where),
        'url'		=> $this->base_url . '/detaillist',
        );
        $this->view->pager = $pager;
        if($this->S_staff['ROLE'] != 'ADMIN')
        {
            $where []= "staffs.id = {$this->S_staff_id}";
        }
        $this->view->contact_histories = $this->model->detail_paginate($where, $order, $pager['curpage'], $pager['perpage']);
        $this->setTitle('所有', $pager['total']);
    }
    /**
	 * @todo 处理相应条件进行处理
	 *
	 */
    public function listAction()
    {
        $where = array();
        if($client = intval($this->_getParam('client', 0)))
        {
            $where []= "client_id = {$client}";
        }
        /**
		 * @todo 处理筛选条件并设置排序方式
		 */
        $order = null;
        /**
		 * @todo 处理筛选字段 并设置默认值
		 */
        /*		$default_statics = array();
        $this->view->statics = $this->model->statics($where, 'trade_type_id', $default_statics);*/
        $date_range = $this->dateRegion('contact_at', $this->_getParam('date_region', 'ALL'), 'contact_histories') ;
        if($date_range) $where []= $date_range;
        $this->view->date_region = $date_region;
        $pager = array(
        'curpage' 	=> $this->_getParam('page', 	1),
        'perpage'	=> $this->_getParam('perpage', 10),
        'total'		=> $this->model->count($where),
        'url'		=> $this->base_url . '/list',
        );
        $this->view->pager = $pager;
       $this->view->contact_histories = $this->model->paginate($where, $order, $pager['curpage'], $pager['perpage']);
        $this->setTitle('所有', $pager['total']);
    }
    /**
	 * @todo 设置相应默认字段
	 *
	 */
    public function addAction()
    {
        $record = array();
        $record['staff_id'] = $this->S_staff['id'];
        if($this->isPost())
        {
            $record =  array_merge($_POST['fields']['contact_history'], $record);
            $record['contact_at'] = $record['report_at'] = REQUEST_TIME;
            if($this->model->insert($record))
            {
                $this->removeCache($record['id']);
                $this->log($msg = "Contact History #{$record['id']} created!");
                $this->feedback($msg, '成功', $this->module_url . '/client/view/id/' . $record['client_id']);
            }
        }
        else
        {
            $record = array_merge($this->model->getDefaults(), $record);
            $record['client_id'] = intval($this->_getParam('client', 0));
            $record['client_id'] || die('参数错误！');
        }
        $this->view->contact_history = $record;
        /**
    	 * 初始化默认参数
    	 */
        $this->setTitle('新增');
    }
    /**
	 * @todo 设置相应默认字段
	 *
	 */
    public function editAction()
    {
        if($this->isPost())
        {
            $record = $_POST['fields']['contact_history'];
            $id = $record['id'];
            /**
    		 * 设置更新时的默认字段
    		 */
            //    		$record['updated_at'] = REQUEST_TIME;
            if($this->model->update($record, "id={$id}"))
            {
                $this->removeCache($id);
                $this->log($msg = "Contact History #{$record['id']} updated!");
                $this->feedback($msg, '成功', $this->base_url);
            }
            $this->view->contact_history = $record;
            $error = 'Update failed!';
        }
        /**
    	 * 若不是POST方法则为初始编辑
    	 */
        $this->setTitle('修改');
        if(empty($error))
        {
            $id = intval($this->_getParam('id', 0));
            $this->view->client = $this->model->find($id);
        }
    }
    /**
	 * @todo 设置相应限制
	 * @todo 出错处理
	 *
	 *
	 */
    public function viewAction()
    {
        if($id = intval($this->_getParam('id', 1)))
        {
            $this->view->contact_history = $contact_history = $this->model->find($id);
            $this->setTitle('查看#' . $contact_history['id']);
        }
        else
        {
            exit(__METHOD__);
        }
    }

    /**
     * 删除到回收站
     * @todo 出错处理
     *
     */
    public function trashAction()
    {
        if(	$id = intval($this->_getParam('id', 1)))
        {
            if($status = $this->model->trashById($id))
            {
                $this->removeCache($id);
                $this->log($msg = "Contact History #{$id} now is marked as deleted!");
                $this->feedback($msg, '成功', $this->base_url);
            }
        }
        exit(__METHOD__);
    }
    /**
     * 从回收站回复删除，应检查操作权限
     * @todo 出错处理
     * @todo 实现权限检查
     *
     */
    public function restoreAction()
    {
        if(	$id = intval($this->_getParam('id', 1)))
        {
            if($status = $this->model->restoreById($id))
            {
                $this->removeCache($id);
                $this->log($msg = "Contact History #{$id} now is marked as deleted!");
                $this->feedback($msg, '成功', $this->base_url);
            }
        }
        exit(__METHOD__);
    }
    /**
     * 彻底删除，应检查操作权限
     * @todo 出错处理
     * @todo 实现权限检查
     *
     */
    public function deleteAction()
    {
        if(	$id = intval($this->_getParam('id', 1)))
        {
            if($status = $this->model->deleteById($id))
            {
                $this->removeCache($id);
                $this->log($msg = "Contact History #{$id} now is marked as deleted!");
                $this->feedback($msg, '成功', $this->base_url);
            }
        }
        exit(__METHOD__);
    }
    /**
     * 批量操作，如设置状态，删除等等
     * @todo 实现主要的操作方式
     */
    public function batchOperateAction()
    {
        /**
    	 * WE do not need render from template!
    	 */
        $this->_helper->viewRenderer->setNoRender();
        if($this->isPost())
        {
            var_dump($_POST);
        }
    }
    /**
     * 搜索接口，将转向list
     *
     */
    public function searchAction()
    {
        $this->_forward('index');
    }
}
